$(document).ready(function() {
    var AJAX_ROUTER = {
        "login": "account/login.do",
        "update": "account/update.do",
        "logout": "account/logout.do"
    };

    var CK_TOKEN = 'token';
    var CK_USERNAME = "id"

    var token = $.cookie(CK_TOKEN);
    var username = $.cookie(CK_USERNAME);

    if (token != null && token != 'null') {
        $("#login-pwd").val("xxxxxxxxxxx");
    }

    if (username != null && username != 'null') {
        $("#login-id").val(username);
    }

    $("#btn-login").click(function() {
        var id = $("#login-id").val();
        var pwd = $("#login-pwd").val();
        var params = {"id": id, "pwd": pwd};
        $.ajax({
            type: "post",
            url: AJAX_ROUTER["login"],
            dataType: "json",
            data: params,
            success : function(result) {
                switch (result.statusCode) {
                    case 200:
                    {
                        var user = result.data;
                        var date = new Date();
                        date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
                        $.cookie(CK_USERNAME, id, {path: '/', expires: date});
                        $.cookie(CK_TOKEN, user.token, {path: '/', expires: date});
                        location.href = "account/my.do";
                        break;
                    }
                    case 400:
                    case 500:
                    {
                        var msg = result.data;
                        $("#model-title").text(msg.title);
                        $("#model-content").text(msg.content);
                        $("#accountModal").modal();
                        break;
                    }
                }
            }
        });
    });

    $(".logout").click(function() {
        $.cookie(CK_TOKEN, null, {path: '/', expires: 1});
    });
});